package com.hao.rabbitmqconsumer.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Slf4j
@Configuration
public class QueueListener {
//    @RabbitListener(queues = "boot_queue")
//    public void ListenerQueue(Message message, Channel channel) throws IOException, InterruptedException {
//        Thread.sleep(1000);
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        // 接收转换消息
//        try {
//            System.out.println(new String(message.getBody()));
//            System.out.println("处理逻辑~~~");
////            int i = 3/0;
//            channel.basicAck(deliveryTag,true);
//        } catch (Exception e) {
//            // 拒绝签收
//            channel.basicNack(deliveryTag,true,true);
//        }
//    }
//   @RabbitListener(queues = "boot_confirm")
//    public void ListenerConfirm(Message message){
//        System.out.println(message);
//        System.out.println(new String(message.getBody()));
//    }
//
//
//    @RabbitListener(queues = "boot_queue")
//    public void QosListener(Message message, Channel channel) throws IOException, InterruptedException {
//        Thread.sleep(1000);
//        //1. 获取消息
//        System.out.println(new String(message.getBody()));
//        //2. 处理业务逻辑
//
//        //3. 签收
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name="dl.queue",declare = "true"),
//            exchange = @Exchange(name = "dl.direct"),
//            key = "dl"
//
//    ))
//    public void listenerDLQueue(Message message,Channel channel) throws IOException {
//        log.info("消费者接收到了dl.queue的延迟消息");
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        // 接收转换消息
//        try {
//            System.out.println(new String(message.getBody()));
////            System.out.println("处理逻辑~~~");
////            int i = 3/0;
//            channel.basicAck(deliveryTag,true);
//        } catch (Exception e) {
//            // 拒绝签收
//            channel.basicNack(deliveryTag,true,false);
//        }
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "delay.queue",durable = "true"),
//            exchange = @Exchange(value = "delay.direct",delayed = "true"),
//            key = "delay"
//    ))
//    public void ListenerDelayQueue(String msg) {
//        log.info("消费者接收到了delay.queue的延迟消息");
//    }
//
//    @RabbitListener(queues = "lazy.queue")
//    public void lazyListener(Message message){
//        System.out.println(new String(message.getBody()));
//    }
    @RabbitListener(queues = "normal.queue")
    public void normalListener(Message message){
        System.out.println(new String(message.getBody()));
    }

}
